package com.njh.charges.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author: njh
 * @date: Created in 2024/1/9 15:35
 * @description: TTL队列
 * @modified By:
 * @version: 1.0
 */
@Component
public class TtlQueueConfig {
    // 普通交换机名称
    public static final String ORDER_EXCHANGE = "zfb_exchange";
    // 死信交换机名称
    public static final String DLX_EXCHANGE = "zfb_dlx_exchange";
    // 普通队列
    public static final String ORDER_QUEUE = "zfb_queue";
    // 死信队列
    public static final String DLX_QUEUE = "zfb_dlx_queue";


    // 声明普通交换机
    @Bean("orderExchange")
    public DirectExchange orderExchange() {
        return new DirectExchange(ORDER_EXCHANGE, true, false);
    }

    // 声明死信交换机
    @Bean("dlxExchange")
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE, true, false);
    }




    // 声明普通队列
    @Bean("orderQueue")
    public Queue orderQueue() {
// .deadLetterRoutingKey("") // 死信路由key
        return QueueBuilder.durable(ORDER_QUEUE)
                .deadLetterExchange(DLX_EXCHANGE) // 死信交换机
                .ttl(60000) // 消息过期时间 1800000
                .build();
    }

    // 死信队列
    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable(DLX_QUEUE).build();
    }


    // 绑定普通队列和交换机
    @Bean
    public Binding orderQueueBindExchange(@Qualifier("orderQueue") Queue queue, @Qualifier("orderExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("");
    }

    // 绑定死信队列和交换机
    @Bean
    public Binding dlxQueueBindExchange(@Qualifier("dlxQueue") Queue queue, @Qualifier("dlxExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("");
    }
}